﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/module")]
    public class ModuleController : BaseController
    {
        [AutoWired]
        private DB_Module DB_Module;


        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/Module/Module_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/Module/Module_Add.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/Module/Module_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ModuleName = ValueConvert.ConvertToString(GetFormParam("ModuleName"));
            string ParentModuleGuid = ValueConvert.ConvertToString(GetFormParam("ParentModuleGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            if (!string.IsNullOrEmpty(ModuleName))
            {
                sql += " and ModuleName like @ModuleName";
                DB_Module.Add2Parameter("ModuleName", "%" + ModuleName + "%");
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(ParentModuleGuid))
                {
                    sql += " and ParentModuleGuid=@ParentModuleGuid";
                    DB_Module.Add2Parameter("ParentModuleGuid", ParentModuleGuid);
                }
            }
            return Result.OK(DB_Module.SelectPage("*", sql, "SortNum desc"));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Module Entity)
        {
            DB_Module.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Module Entity)
        {
            DB_Module.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ModuleGuid = ValueConvert.ConvertToString(GetFormParam("ModuleGuid"));
            return Result.OK(DB_Module.SelectOne(ModuleGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var item in KeyIDList)
            {
                Frame_Module module = DB_Module.SelectOne(item);
                DB_Module.Add2Parameter("ParentModuleGuid", module.ParentModuleGuid);
                int SubMoudleCount = DB_Module.SelectCount("ParentModuleGuid=@ParentModuleGuid");
                if (SubMoudleCount > 0)
                {
                    return Result.Error(string.Format("模块{0}包含子模块，无法删除", module.ModuleName));
                }
            }
            DB_Module.DeleteBatch(KeyIDList);
            return Result.OK();
        }

        [Route("tree"), HttpPost]
        public IActionResult Tree()
        {
            List<Frame_Module> ModuleList = DB_Module.SelectAllList();
            ModuleList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            Frame_Module Root = new Frame_Module();
            Root.ModuleGuid = "Top";
            Root.ModuleName = "全部模块";
            Root.ParentModuleGuid = "";
            ModuleList.Add(Root);
            foreach (Frame_Module item in ModuleList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.ModuleGuid;
                node.Name = item.ModuleName;
                node.PID = item.ParentModuleGuid;
                if (item.ModuleGuid == Root.ModuleGuid)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }

        [Route("getMenuRightTree"), HttpPost]
        public IActionResult GetMenuRightTree()
        {
            List<Frame_Module> ModuleList = DB_Module.SelectAllList();
            ModuleList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            foreach (Frame_Module item in ModuleList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.ModuleGuid;
                node.Name = item.ModuleName;
                node.PID = item.ParentModuleGuid;
                if (item.ParentModuleGuid == "Top")
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                node.AddExtra("ModuleUrl", item.ModuleUrl);
                node.AddExtra("ModuleIcon", item.ModuleIcon);
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }
    }
}
